<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>走动时间格式化</title>
</head>
<body>
    <p></p>
    <script>
        var p = document.getElementsByTagName('p')[0];

        function time(){
            // 创建当前时间
            var date = new Date();

            // 年月日时分秒星期
            var year = date.getFullYear();

            // 个位数补0
            var month = date.getMonth()+1;
            month = month<10?'0'+month:month;

            var day = date.getDate();
            day = day<10?'0'+day:day;

            var hour = date.getHours();
            hour = hour<10?'0'+hour:hour;

            var minute = date.getMinutes();
            minute = minute<10?'0'+minute:minute;

            var second = date.getSeconds();
            second = second<10?'0'+second:second;

            var week = date.getDay();

            // 根据星期索引确定星期几
            var weekArr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六',]

            return year+'-'+month+'-'+day+' '+ hour+':'+minute+':'+second+ ' ' +weekArr[week];
        }

        // 初始话
        p.innerHTML = time();

        // 定时器写入页面
        setInterval(function(){
            p.innerHTML = time();
        },1000)
        
    </script>
</body>
</html>